package com.example.testdemo.demos.web.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Config.ResultEnum;
import com.example.testdemo.demos.web.Dao.ShippingAddressDao;
import com.example.testdemo.demos.web.Entity.ShippingAddress;
import com.example.testdemo.demos.web.Service.ShippingAddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShippingAddressServiceImpl implements ShippingAddressService {
    @Resource
    private ShippingAddressDao addressDao;

    /**
     * 新增用户收货地址
     */
    @Override
    public Result addAddress(ShippingAddress address){
        Result result = new Result();
        try {
          int res =addressDao.insert(address);
          if (res==1){
              result.setCode(ResultEnum.SUCCESS.getCode());
              result.setMsg("收货地址添加成功");
          }else {
              result.setCode(ResultEnum.BADREQUEST.getCode());
              result.setMsg("收货地址添加失败");
          }
        }catch (Exception e){
            result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
            result.setData(e);
            result.setMsg(ResultEnum.DB_UPDATE_FAILURE.getData());
        }
        return result;
    }

    /**
     * 删除用户收货地址
     */
    @Override
    public Result delAddress(ShippingAddress address){
        Result result = new Result();
        int res = addressDao.deleteById(address);
        if (res==1){
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("收货地址删除成功");
        }else {
            result.setCode(ResultEnum.BADREQUEST.getCode());
            result.setMsg("收货地址删除失败");
        }
        return result;
    }

    /**
     * 查询用户收货地址
     */
    @Override
    public Result selAddress(ShippingAddress address){
        Result result = new Result();
        LambdaQueryWrapper<ShippingAddress> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShippingAddress::getUserId,address.getUserId());
        List<ShippingAddress> list = addressDao.selectList(lqw);
        if (!list.isEmpty()){
            result.setMsg("查询成功");
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(list);
        }else {
            result.setMsg("暂无数据");
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(list);
        }
        return result;
    }

    /**
     * 修改用户收货地址
     */
    @Override
    public Result setAddress(ShippingAddress address){
        Result result = new Result();
        int res = addressDao.updateById(address);
        if (res==1){
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("收货地址修改成功");
        }else {
            result.setCode(ResultEnum.BADREQUEST.getCode());
            result.setMsg("收货地址修改失败");
        }
        return result;
    }


}
